//观察者模式
class Subject {
  private static status: number
  private static observers: Obeserver[] = []
  //获取状态
  getStatus() {
    return Subject.status
  }
  //设置状态
  setStatus(status: number) {
    Subject.status = status
    this.notify() //状态改变，通知观察者
  }
  //添加观察者
  addObserver(observer: Obeserver) {
    Subject.observers.push(observer)
  }
  //通知观察者
  notify() {
    Subject.observers.forEach((observer) => {
      observer.update()
    })
  }
}
class Obeserver {
  name: string
  constructor(name: string) {
    this.name = name
  }
  update() {
    console.log(`${this.name}收到通知`)
  }
}
const subject = new Subject()
const observer1 = new Obeserver('观察者1')
const observer2 = new Obeserver('观察者2')
subject.addObserver(observer1)
subject.addObserver(observer2)
subject.setStatus(1)
subject.setStatus(2)
